package com.kyxx.topics.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSONObject;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.net.LoadingStringCallBack;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.StringUtil;
import com.kyxx.topics.utils.ToastUtils;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;

import org.json.JSONException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/22.
 */
public class FeedBackActivity2 extends BaseActivity {
    @Bind(R.id.ed_feed_back_content)
    EditText ed_feed_back_content;
    @Bind(R.id.ed_feed_back_num)
    EditText ed_feed_back_num;
    @Bind(R.id.avi)
    ProgressBar avi;
    @Bind(R.id.ll_no)
    LinearLayout llNo;
    @Bind(R.id.ll_reload)
    LinearLayout llReload;
    @Bind(R.id.rl_progress)
    RelativeLayout rl_progress;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_feed_back2);
        ButterKnife.bind(this);
    }

    @Override
    protected void findViewById() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic() {
        rl_progress.setBackgroundColor(Color.argb(80,255,255,255));
    }


    @OnClick({R.id.iv_back, R.id.tv_feed_back_send})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_feed_back_send:
                if(StringUtil.isEmpty(ed_feed_back_content.getText().toString().trim())){
                    ToastUtils.show(context,"请填写您的意见");
                    return;
                }
                rl_progress.setVisibility(View.VISIBLE);
                sendMessage();
                break;
        }
    }

    private void sendMessage() {
        HttpParams params = new HttpParams();
        JSONObject obj = new JSONObject();
        obj.put("uid", BaseApp.getModel().getId());
        obj.put("title", ed_feed_back_num.getText().toString().trim());
        obj.put("content", ed_feed_back_content.getText().toString().trim());
        params.put("json", obj.toString());
        OkGoRequest.post(UrlUtils.FEED_BACK, context, "vip_card_get_all_type", CacheMode.NO_CACHE, params, new LoadingStringCallBack(FeedBackActivity2.this,true) {
            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                rl_progress.setVisibility(View.GONE);
                try {
                    org.json.JSONObject result = new org.json.JSONObject(s);
                    ToastUtils.show(context, result.getString("msg"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                ToastUtils.show(context, "网络错误请稍后再试");
                rl_progress.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
